/**************************************************************************
Copyright (C) jianbo miao Corporation.  All Rights Released.
this is a 3d engin named our_3d_engin.
our_3d_engin feature:
1:there is no float.
2:it do the 3d things all by softwear.
3:base on 1 and 2 , it can execution on arm which have no 3d hardwear accelerate.

  if you have any  suggestion or question,pls contact with me
  mail:miaojb@126.com
  msn:miaojianbo@hotmail.com
  qq:30209027
  
    2008/01/01
***************************************************************************/

#ifndef OUR_MATH
#define OUR_MATH
//================================================================================================
//================================================================================================

#include "our_cpu.h"
//=============================================

// TYPES //////////////////////////////////////////////////

//=================================================================================================

// 3D vector, point without the w -----------------------------------------------
//3*4=12B
typedef union 
{
    FIXP16 M[3];
    struct 
    {
        FIXP16 x,y,z;
    }MM; 
} FIX_VECTOR3D, FIX_POINT3D, *FIX_VECTOR3D_PTR, *FIX_POINT3D_PTR;
// 3x3 matrix -----------------------------------------------

typedef union
{
    FIXP16 M[3][3]; // array indexed data storage
    
    // storage in row major form with explicit names
    struct 
    {
        FIXP16 M00, M01, M02;
        FIXP16 M10, M11, M12;
        FIXP16 M20, M21, M22;
    }MM; 
} FIX_MATRIX_3X3, *FIX_MATRIX_3X3_PTR;

//=================================================================================================

// 2x2 matrix -----------------------------------------------
typedef union
{
    FIXP16 M[2][2]; // array indexed data storage
    
    // storage in row major form with explicit names
    struct 
    {
        FIXP16 M00, M01;
        FIXP16 M10, M11;
    }MM; 
} FIX_MATRIX_2X2, *FIX_MATRIX_2X2_PTR;

// 1x4 matrix -----------------------------------------------
typedef union
{
    FIXP16 M[4];
    struct 
    {
        FIXP16 M00, M01, M02, M03;
    }MM; 
} FIX_MATRIX_1X4, *FIX_MATRIX_1X4_PTR;

// 4x4 matrix -----------------------------------------------
typedef union
{
    FIXP16 M[4][4];
    struct 
    {
        FIXP16 M00, M01, M02, M03;
        FIXP16 M10, M11, M12, M13;
        FIXP16 M20, M21, M22, M23;
        FIXP16 M30, M31, M32, M33;
    }MM;
} FIX_MATRIX_4X4, *FIX_MATRIX_4X4_PTR;

// 4x3 matrix -----------------------------------------------

typedef union
{
    FIXP16 M[4][3];
    struct 
    {
        FIXP16 M00, M01, M02;
        FIXP16 M10, M11, M12;
        FIXP16 M20, M21, M22;
        FIXP16 M30, M31, M32;
    }MM;
} FIX_MATRIX_4X3, *FIX_MATRIX_4X3_PTR;

// vector types -----------------------------------------------

// 2D vector, point without the w ////////////////////////
typedef union
{
    FIXP16 M[2];
    struct 
    {
        FIXP16 x,y;
    }MM; 
}FIX_VECTOR2D, FIX_POINT2D, *FIX_VECTOR2D_PTR, *FIX_POINT2D_PTR;

// 4D homogenous vector, point with w-----------------------------------------------
typedef union
{
    FIXP16 M[4];
    struct 
    {
        FIXP16 x,y,z,w;
    }MM; 
} FIX_VECTOR4D, FIX_POINT4D, *FIX_VECTOR4D_PTR, *FIX_POINT4D_PTR;

//============================================================================
// used for swapping algorithm
#define OUR_SWAP(a,b,t) {t=a; a=b; b=t;}
#define OUR_int_abs(a) ((a)>0 ? (a) : (0-a))
//============================================================================
void 	OUR_Build_Sin_Cos_Tables(void);
/*
if you want to use the functions our_fast_sin or our_fast_cos,do this function at init.
*/
//============================================================================
FIXP16 	OUR_Fast_Sin(FIXP16 theta);
//============================================================================
FIXP16 	OUR_Fast_Cos(FIXP16 theta);
//============================================================================
void 	OUR_FIX_Mat_Mul_VECTOR3D_3X3(	FIX_VECTOR3D_PTR  	va, 
                                     FIX_MATRIX_3X3_PTR 	mb,
                                     FIX_VECTOR3D_PTR  	vprod);
//============================================================================

UINT 	OUR_sqrt_16(unsigned long M);
//============================================================================
void 	OUR_FIX_Mat_Mul_3X3(	FIX_MATRIX_3X3_PTR  	ma, 
                            FIX_MATRIX_3X3_PTR 	mb,
                            FIX_MATRIX_3X3_PTR  	mprod);
//============================================================================
void 	OUR_FIX_VECTOR3D_SUB(	FIX_VECTOR3D_PTR  	va, 
                             FIX_VECTOR3D_PTR  	vb,
                             FIX_VECTOR3D_PTR  	vprod);
//============================================================================
void 	OUR_FIX_VECTOR3D_ADD(	FIX_VECTOR3D_PTR  	va, 					//20t
                             FIX_VECTOR3D_PTR  	vb,
                             FIX_VECTOR3D_PTR  	vprod);
//============================================================================
void 	OUR_VECTOR3D_Normalize(FIX_VECTOR3D_PTR va);
void OUR_FAST_Normalize(FIX_VECTOR3D_PTR va)	;
FIXP16 	OUR_VECTOR3D_Dot(FIX_VECTOR3D_PTR va, FIX_VECTOR3D_PTR vb);
void 	OUR_VECTOR3D_Cross(		FIX_VECTOR3D_PTR va, 
                           FIX_VECTOR3D_PTR vb,
                           FIX_VECTOR3D_PTR vn);
int 	OUR_Mat_Inverse_3X3(FIX_MATRIX_3X3_PTR m, FIX_MATRIX_3X3_PTR mi);
int 	OUR_FAST_Length(FIX_VECTOR3D_PTR va);	
UINT OUR_Fast_reciprocal(FIXP16 theta);
void OUR_Build_reciprocal_look(void);
UINT OUR_Fast_division(FIXP16 y,FIXP16 x);
//================================================================================================
void OUR_mmcpy(UINT *des,UINT *src,UINT num);
void OUR_mmset(UINT *des,UINT src,UINT num);
void OUR_mmset_box16(int x,int y,int w,int h,UCHAR *video_buffer,int lpitch,CLR c);
void OUR_BOX_OUT_clear16(int x1,int y1,
                                        CLR C,
                                        UCHAR *video_buffer, int lpitch);    
//================================================================================================
//extern unsigned int reciprocal_look[1000];
//================================================================================================



#endif
